<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------


/**
 * 系统配置类
 * @package  module/base/system
 * @author   allen.qiang
 */

class sysConfig {

    private $dbca;

    public function __construct( &$dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 得到配置组数据
     * @param null|int $config_group_id 配置ID
     * @return array|null
     */
    public function getGroup( $config_group_id = null ) {

        if (is_null($config_group_id)) {
            $search_sql = 'SELECT * FROM sys_config_group ORDER BY sort ASC';
            $group_array = $this->dbca->stmt_query($search_sql);
        } else {
            $search_sql = 'SELECT * FROM sys_config_group WHERE config_group_id = ?';
            $group_array = $this->dbca->stmt_query($search_sql, 'i', $config_group_id);
        }
        if (!$group_array) {
            return null;
        }

        foreach($group_array as &$item) {
            $item_array[] = array(
                'config_group_id' => $item->config_group_id,
                'config_group_name' => $item->config_group_name,
                'config_group_file' => $item->config_group_file,
            );
        }

        if (is_null($config_group_id)) {
            return $item_array;
        } else {
            return $item_array[0];
        }

    }

    /**
     * 检查配置组
     * @param $key
     * @return bool
     */
    public function checkGroup( $key ) {
        if (isset($this->group_array[$key])) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查询配置信息
     * @param array $arg_array
     * @return array|null
     */
    public function searchConfig( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        if (isset($arg_array['config_key']) && !empty($arg_array['config_key'])) {
            $sql .= ' AND config_key = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['config_key'];
        }

        if (isset($arg_array['config_group_id']) && is_numeric($arg_array['config_group_id'])) {
            $sql .= ' AND config_group_id = ?';
            $params_type .= 'i';
            $params_array[] = trim($arg_array['config_group_id']);
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname'])) {
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_config ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'config_title' => $item->config_title,
                    'config_key' => $item->config_key,
                    'config_value' => $item->config_value,
                    'config_group' => $item->config_group,
                    'config_type' => $item->config_type,
                    'config_sort' => $item->config_sort
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 更新配置信息
     * @param string $config_key
     * @param mixed $config_value
     * @param string $config_group_id
     * @return array
     */
    public function updateConfig( $config_key, $config_value, $config_group_id) {

        if (!isset($config_key, $config_value, $config_group_id) || empty($config_key) || !is_numeric($config_group_id)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }
        if ($config_value !== '0' && empty($config_value)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            //更新配置信息
            $config_obj = new entity('sys_config');
            $config_obj->config_key = $config_key;
            $config_obj->config_group_id = $config_group_id;
            if (!$this->dbca->fetch($config_obj)) {
                throw new Exception('配置信息没有找到', 404);
            }
            $config_obj->config_value = $config_value;
            $this->dbca->save($config_obj);
            return array( 'code' => 200, 'message' => 'ok' );

        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

    /**
     * 修改配置文件内容
     * @param int $config_group_id 配置组ID
     * @param string $config_group_file 配置文件名
     * @return array|bool
     */
    public function editConfigFile( $config_group_id, $config_group_file ) {

        if (!isset($config_group_id, $config_group_file) || !is_numeric($config_group_id) || empty($config_group_file)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        //查询配置信息
        $arg_array = array(
            'config_group_id' => $config_group_id,
            'sortname' => 'config_sort',
            'sortorder' => 'ASC'
        );
        $config_array = $this->searchConfig( $arg_array );
        if (!$config_array) {
            return array( 'code' => 404, 'message' => '配置信息没有找到' );
        }

        //生成配置文件内容
        $file_content = '<?php' . "\n";
        foreach($config_array as &$config) {
            $file_content .= sprintf('define("%s", "%s");', $config['config_key'], $config['config_value']) . "\n";
        }

        //文件绝对地址
        $file_address = G_CONFIG_ROOT . 'config.' . $config_group_file . '.php';

        //加载文件类
        quanli_require('module/base/common/quanliFile.class.php');

        //实例文件类
        $quanliFile = new quanliFile();

        //修改文件内容
        return $quanliFile->write($file_address, $file_content);

    }

}